// import 'package:flexible/flexible.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

double flexible(BuildContext context, double xx) {
  return xx;
}

/// 输入的TextEditing的封装 包括数据上报的部分
///
enum CustomInputCellStyle {
  defaultStyle,

  ///有外部边框的类型 为 6
  style1,

  ///无外部边框的类型
  style2,

  ///有外部边框的类型 为 4
  style3,

  ///中学习题PC输入框样式
  style4,
}

/// 输入内容
/// @owner dansun
/// @groupInfo [ComponentGroupInfo.input]
class CustomInputCell extends StatelessWidget {
  CustomInputCell(
      {Key key,
      this.actionWidget,
      this.leadingWidget,
      this.cellHeight = 52,
      this.hintText,
      this.maxLines = 1,
      this.controller,
      this.inputFormatterList,
      this.focusNode,
      this.onChanged,
      this.onSubmitted,
      this.decoration,
      this.style,
      this.obscureText = false,
      this.enable = true,
      this.keyboardType,
      this.name,
      this.extraData,
      this.isHasDivider = true,
      this.outDecoration,
      this.outPadding,
      this.maxLength,
      this.textAlign,
      this.buildCounter,
      this.customInputCellStyle = CustomInputCellStyle.defaultStyle,
      this.enableInteractiveSelection = true})
      : super(key: key);

  // 中学版练习的输入框样式
  CustomInputCell.style1(
      {Key key,
      this.actionWidget,
      this.leadingWidget,
      this.cellHeight = 33.0,
      this.hintText,
      this.maxLines = 1,
      this.controller,
      this.inputFormatterList,
      this.focusNode,
      this.onChanged,
      this.onSubmitted,
      this.style,
      this.obscureText = false,
      this.enable = true,
      this.keyboardType,
      this.name,
      this.extraData,
      this.isHasDivider = true,
      this.maxLength = 300,
      this.buildCounter,
      this.textAlign,
      this.outDecoration = const BoxDecoration(border: Border.fromBorderSide(BorderSide(width: 0.5))),
      this.customInputCellStyle = CustomInputCellStyle.style4,
      this.outPadding = const EdgeInsets.symmetric(horizontal: 4.0),
      this.decoration = decorationSmall,
      this.enableInteractiveSelection = true})
      : super(key: key);

  static const InputDecoration decorationSmall = InputDecoration(
    border: InputBorder.none,
    focusedBorder: InputBorder.none,
    focusedErrorBorder: InputBorder.none,
    filled: false,
    isCollapsed: true,
    contentPadding: EdgeInsets.only(bottom: -8.0),
    //Change this value to custom as you like
    isDense: true,
  );

  static const InputDecoration decorationLarge = InputDecoration(
    border: InputBorder.none,
    focusedBorder: InputBorder.none,
    focusedErrorBorder: InputBorder.none,
    filled: false,
    isCollapsed: true,
    contentPadding: EdgeInsets.only(bottom: -10.0),
    //Change this value to custom as you like
    isDense: true,
  );

  /// 左侧widget
  final Widget leadingWidget;
  final InputDecoration decoration;

  /// 文字样式
  final TextStyle style;

  ///右侧widget
  final Widget actionWidget;

  ///行高
  final double cellHeight;

  ///提示信息
  final String hintText;

  final int maxLines;

  final TextEditingController controller;
  final FocusNode focusNode;

  final List<TextInputFormatter> inputFormatterList;
  final TextInputType keyboardType;
  final bool obscureText;

  ///底部分割线
  final bool isHasDivider;
  final bool enable;

  ///长按输出面板
  final bool enableInteractiveSelection;

  final int maxLength;

  final ValueChanged<String> onChanged;
  final ValueChanged<String> onSubmitted;

  /// 控件名字
  final String name;

  /// 附加的上报信息
  final Map<String, dynamic> extraData;

  final CustomInputCellStyle customInputCellStyle;

  ///
  final InputCounterWidgetBuilder buildCounter;
  final Decoration outDecoration;
  final EdgeInsetsGeometry outPadding;

  final TextAlign textAlign;

  double _getBorderRadius(BuildContext context) {
    if (customInputCellStyle == CustomInputCellStyle.style1) {
      return flexible(context, 6.0);
    }
    if (customInputCellStyle == CustomInputCellStyle.style3) {
      return flexible(context, 4.0);
    }
    return 0;
  }

  @override
  Widget build(BuildContext context) {
    final bool isDivider = isHasDivider && customInputCellStyle != CustomInputCellStyle.style1;
    return Container(
      decoration: customInputCellStyle == CustomInputCellStyle.style2 || customInputCellStyle == CustomInputCellStyle.defaultStyle
          ? null
          : BoxDecoration(
              border: Border.all(
                color: Color(0xFFE6E6E6),
              ),
              borderRadius: BorderRadius.circular(
                _getBorderRadius(context),
              ),
              color: Colors.white,
            ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(_getBorderRadius(context)),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Row(
              mainAxisAlignment: actionWidget != null ? MainAxisAlignment.spaceBetween : MainAxisAlignment.start,
              children: <Widget>[
                leadingWidget ?? Container(),
                Expanded(
                  child: Container(
                    alignment: Alignment.centerLeft,
                    decoration: outDecoration ?? BoxDecoration(color: Colors.white),
                    height: flexible(context, cellHeight),
                    padding: outPadding,
                    child: TextField(
                      maxLines: maxLines,
                      enableInteractiveSelection: enableInteractiveSelection,
                      controller: controller,
                      focusNode: focusNode,
                      enabled: enable,
                      textAlign: textAlign ?? TextAlign.start,
                      obscureText: obscureText,
                      cursorColor: Color(0xFF8B5FFE),
                      onChanged: onChanged,
                      onSubmitted: onSubmitted,
                      maxLength: maxLength ?? TextField.noMaxLength,
                      buildCounter: buildCounter ??
                          (BuildContext context, {int currentLength, bool isFocused, int maxLength}) {
                            return Container(
                              width: 0.0,
                              height: 0.0,
                            );
                          },
                      style: style ??
                          TextStyle(
                              textBaseline: TextBaseline.alphabetic,
                              fontSize: flexible(
                                context,
                                17.0,
                              ),
                              color: Colors.black),
                      decoration: decoration ??
                          InputDecoration(
                            hintText: hintText,
                            hintStyle: TextStyle(
                              fontSize: flexible(context, 16.0),
                              color: Colors.black.withOpacity(0.4),
                            ),
                            isCollapsed: true,
                            border: InputBorder.none,
                            focusedBorder: InputBorder.none,
                            focusedErrorBorder: InputBorder.none,
                            filled: false,
                            contentPadding: EdgeInsets.only(bottom: -7.0, left: flexible(context, 11)),
                          ),
                      inputFormatters: inputFormatterList,
                      keyboardType: keyboardType,
                    ),
                  ),
                ),
                actionWidget ?? Container()
              ],
            ),
            if (isDivider)
              Container(
                height: 1.0,
                color: Color(0xFFE6E6E6),
              )
            else
              Container(),
          ],
        ),
      ),
    );
  }
}
